package cn.jh.smartos.activity

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.support.v4.app.FragmentTransaction
import android.view.KeyEvent
import cn.jh.smartos.R
import cn.jh.smartos.TabEntity
import cn.jh.smartos.bean.ConnConfig
import cn.jh.smartos.fragment.SettingF
import cn.jh.smartos.fragment.MsgF
import cn.jh.smartos.utils.ToastU
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener

import kotlinx.android.synthetic.main.activity_main.*
import java.util.ArrayList

class MainActivity : BaseActivity() {
    override fun initData() {


    }

    //默认为0
    private var mIndex = 1
    private var mTitles = arrayOf("")
    override fun layoutId(): Int = R.layout.activity_main
    // 未被选中的图标
    private val mIconUnSelectIds = intArrayOf(R.mipmap.ic_discovery_normal, R.mipmap.ic_hot_normal)
    // 被选中的图标
    private val mIconSelectIds = intArrayOf(R.mipmap.ic_discovery_selected, R.mipmap.ic_hot_selected)

    override fun onCreate(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            mIndex = savedInstanceState.getInt("currTabIndex")
        }
        if (ConnConfig.isService) {
            mTitles = arrayOf("消息", "服务器")

        } else {
            mTitles = arrayOf("消息", "客户端")
        }
        super.onCreate(savedInstanceState)
        initTab()
    }

    private val mHandler: Handler by lazy {
        Handler(Looper.myLooper())
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {

        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }

    private val mTabEntities = ArrayList<CustomTabEntity>()
    //初始化底部菜单
    private fun initTab() {
        (0 until mTitles.size)
                .mapTo(mTabEntities) { TabEntity(mTitles[it], mIconSelectIds[it], mIconUnSelectIds[it]) }
        //为Tab赋值
        tab_layout.setTabData(mTabEntities)
        tab_layout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                //切换Fragment
                switchFragment(position)
            }

            override fun onTabReselect(position: Int) {

            }
        })
        tab_layout.currentTab = mIndex
        switchFragment(mIndex)

    }


    private fun switchFragment(position: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        when (position) {
            0 -> {
                msgFragment?.let {
                    transaction.show(it)
                } ?: MsgF.getInstance(mTitles[position]).let {
                    msgFragment = it
                    transaction.add(R.id.fl_container, it, "msg")
                }
            }


            1  //发现
            -> mServiceFragment?.let {
                transaction.show(it)
            } ?: SettingF.getInstance(mTitles[position]).let {
                mServiceFragment = it
                transaction.add(R.id.fl_container, it, "service_n")
            }


        }


        transaction.commitAllowingStateLoss()
    }

    private var mServiceFragment: SettingF? = null
    private var msgFragment: MsgF? = null
    /**
     * 隐藏所有的Fragment
     * @param transaction transaction
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mServiceFragment?.let { transaction.hide(it) }
        msgFragment?.let { transaction.hide(it) }

    }

    private var mExitTime: Long = 0

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 2000) {
                finish()
            } else {
                mExitTime = System.currentTimeMillis()
//                ("再按一次退出程序")
                ToastU.showToast("再按一次退出程序")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

}
